package com.dhcc.sdc.cdss.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.sdc.base.entity.SdcQcCdssLinkEntity;
import com.dhcc.sdc.base.service.ICdssStandardMatchService;
import com.dhcc.sdc.base.service.ISdcQcCdssLinkService;
import com.dhcc.sdc.busi.entity.SdcQcEntity;
import com.dhcc.sdc.busi.service.ISdcQcService;
import com.dhcc.sdc.cdss.dao.SdcCdssEvalMapper;
import com.dhcc.sdc.cdss.entity.SdcCdssEvalEntity;
import com.dhcc.sdc.cdss.service.ISdcCdssEvalService;
import com.dhcc.sds.standard.entity.SdsStandSourceEntity;
import com.dhcc.sds.standard.entity.SdsStandSourceFieldEntity;
import com.dhcc.sds.standard.service.ISdsStandSourceFieldService;
import com.dhcc.sds.standard.service.ISdsStandSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

/**
 * 表数据服务层接口
 *
 * @ClassName: SdcCdssEvalServiceImpl
 * @Description: TODO
 * @author zhangdc
 * @date 2022-06-23 16:36:24
 */
@Service("sdcCdssEvalService")
public class SdcCdssEvalServiceImpl extends BaseServiceImpl<SdcCdssEvalMapper, SdcCdssEvalEntity> implements ISdcCdssEvalService {

    @Autowired
    private ISdcQcService sdcQcService;
    @Autowired
    private ISdsStandSourceService standSourceService;
    @Autowired
    private ISdsStandSourceFieldService sdsStandSourceFieldService;
    @Autowired
    private ICdssStandardMatchService cdssStandardMatchService;
    @Autowired
    private ISdcQcCdssLinkService sdcQcCdssLinkService;

    @Override
    public List<SdcCdssEvalEntity> page(Page<SdcCdssEvalEntity> page, Map<String, Object> map) {
        List<SdcCdssEvalEntity> page1 = this.baseMapper.page(page, map);
        for (SdcCdssEvalEntity cdssEvalEntity : page1){
            SdcQcCdssLinkEntity sdcQcCdssLinkEntity = sdcQcCdssLinkService.selectByNodeId(cdssEvalEntity.getNodeId());
            if (!StringUtils.isEmpty(sdcQcCdssLinkEntity)){
                SdcQcEntity sdcQcEntity = sdcQcService.selectById(sdcQcCdssLinkEntity.getQcItemId());
                if (!StringUtils.isEmpty(sdcQcEntity)){
                    cdssEvalEntity.setQcDesc(sdcQcEntity.getQcDesc());
                }
            }
            if (!StringUtils.isEmpty(cdssEvalEntity.getDataSource()) && !StringUtils.isEmpty(cdssEvalEntity.getDataSourceItem())){
                String referenceTime = cdssStandardMatchService.handleReferenceTime(SdcCdssEvalEntity.class, cdssEvalEntity);
                //String referenceTime = handleItemName(SdcCdssEvalEntity.class, cdssEvalEntity);
                cdssEvalEntity.setReferenceTime(referenceTime);
            }
        }
        return page1;
    }

    @Override
    public List<SdcCdssEvalEntity> list(Map<String, Object> map) {
        return this.baseMapper.list(map);
    }

    @Override
    public String handleJsonArrayData(JSONArray jsonArray, Long qcEntityId, List<String> cdssEvalNodeIds) {
        String res;
        int count = 0;
        List<SdcCdssEvalEntity> cdssEvalEntities = JSONObject.parseArray(jsonArray.toJSONString(), SdcCdssEvalEntity.class);
        for (SdcCdssEvalEntity cdssEvalEntity : cdssEvalEntities){
            SdcCdssEvalEntity cdssEvalEntityNew = new SdcCdssEvalEntity();
            cdssEvalEntityNew.setQcentityId(qcEntityId);
            cdssEvalEntityNew.setItemId(cdssEvalEntity.getItemId());
            cdssEvalEntityNew.setNodeId(cdssEvalEntity.getNodeId());
            cdssEvalNodeIds.add(cdssEvalEntity.getNodeId());
            SdcCdssEvalEntity cdssEvalEntity1 = this.baseMapper.selectOne(cdssEvalEntityNew);
            if (StringUtils.isEmpty(cdssEvalEntity1)){
                //insert
                cdssEvalEntity.setQcentityId(qcEntityId);
                cdssEvalEntity.setIsCdss(1);
                boolean insert = cdssEvalEntity.insert();
                if (insert){
                    count++;
                }
            }else {
                //update
                cdssEvalEntity1.setQcentityId(qcEntityId);
                cdssEvalEntity1.setItemDesc(cdssEvalEntity.getItemDesc());
                cdssEvalEntity1.setItemName(cdssEvalEntity.getItemName());
                cdssEvalEntity1.setSourceFlag(cdssEvalEntity.getSourceFlag());
                cdssEvalEntity1.setNoCondition(cdssEvalEntity.getNoCondition());
                cdssEvalEntity1.setCureId(cdssEvalEntity.getCureId());
                boolean b = cdssEvalEntity1.updateAllColumnById();
                if (b){
                    count++;
                }
            }
        }
        res = "新增/更新" + count + "条";
        return res;
    }

    @Override
    public List<Map<String, Object>> sdcItemList(Map<String, Object> map) {
        return this.baseMapper.sdcItemList(map);
    }

    @Override
    public List<String> selectNodeIds(Long entityId) {
        return this.baseMapper.selectNodeIds(entityId);
    }

    @Override
    public void deleteByNodeIds(List<String> nodeIds) {
        this.baseMapper.deleteByNodeIds(nodeIds);
    }

    private <T> String handleItemName(Class<T> clazz, T cla){
        String name = "";
        T u = clazz.cast(cla);
        String itemName = null,param = null;
        Long dataSource = null,dataSourceItem = null;
        Integer pushStart = null,pushEnd = null,timeUnit = null;
        for (Field field : u.getClass().getDeclaredFields()){
            field.setAccessible(true);
            try {
                if(field.getName().equals("itemName")){
                    itemName = field.get(u)==null?null:String.valueOf(field.get(u));
                }else if(field.getName().equals("dataSource")){
                    dataSource = field.get(u)==null?null: Long.valueOf(String.valueOf(field.get(u)));
                }else if(field.getName().equals("param")){
                    param = field.get(u)==null?null:String.valueOf(field.get(u));
                }else if(field.getName().equals("dataSourceItem")){
                    dataSourceItem = field.get(u)==null?null: Long.valueOf(String.valueOf(field.get(u)));
                }else if(field.getName().equals("pushStart")){
                    pushStart = field.get(u)==null?null: Integer.valueOf(String.valueOf(field.get(u)));
                }else if(field.getName().equals("pushEnd")){
                    pushEnd = field.get(u)==null?null: Integer.valueOf(String.valueOf(field.get(u)));
                }else if(field.getName().equals("timeUnit")){
                    timeUnit = field.get(u)==null?null: Integer.valueOf(String.valueOf(field.get(u)));
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        if (dataSource != null){
            SdsStandSourceEntity sourceEntity = standSourceService.selectById(dataSource);
            name = itemName + "[" + sourceEntity.getSourceDesc();
        }
        if (dataSourceItem != null){
            SdsStandSourceFieldEntity sourceFieldEntity = sdsStandSourceFieldService.selectById(dataSourceItem);
            name = name + ":" + sourceFieldEntity.getFieldDesc();
        }
        if (param != null){
            name = name + " (" + param + ")";
        }

        String timeUnitStr = "";
        //1year、2month、3day、 4hour、5minute、6second
        if (timeUnit == 1){
            timeUnitStr = "year";
        }else if (timeUnit == 2){
            timeUnitStr = "month";
        }else if (timeUnit == 3){
            timeUnitStr = "day";
        }else if (timeUnit == 4){
            timeUnitStr = "hour";
        }else if (timeUnit == 5){
            timeUnitStr = "minute";
        }else if (timeUnit == 6){
            timeUnitStr = "second";
        }
        if (pushStart == null && pushEnd == null){
            name = name + "]";
        }else {
            if (pushStart != null){
                name = name + " " + pushStart;
            }else {
                name = name + " ";
            }
            if (pushEnd != null){
                name = name + "~" + pushEnd + timeUnitStr + "]";
            }else {
                name = name + "~" + " " + timeUnitStr + "]";
            }
        }

        return name;
    }
}
